package com.shuai.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuai.model.bo.AllotBo;
import com.shuai.model.form.AllotForm;
import com.shuai.model.po.SysRole;
import com.shuai.model.query.SysRoleQuery;
import com.shuai.model.vo.SysRoleVo;
import com.shuai.service.SysRoleService;
import com.shuai.utils.returnVo.ReturnModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息 前端控制器
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 添加角色信息
     * @param sysRole
     * @return
     */
    @PostMapping("/addSysRole")
    public ReturnModel<Object> addSysRole(@RequestBody SysRole sysRole){
        sysRoleService.addSysRole(sysRole);
        return ReturnModel.successInstance();
    }

    /**
     * 删除角色信息
     * @param query
     * @return
     */
    @RequestMapping ("/deleteSysRole")
    public ReturnModel<Object> deleteSysRole(@RequestBody SysRoleQuery query){
        sysRoleService.deleteSysRole(query);
        return ReturnModel.successInstance();
    }

    /**
     * 更新角色信息
     * @param sysRole
     * @return
     */
    @PostMapping("/editSysRole")
    public ReturnModel<Object> editSysRole(@RequestBody SysRole sysRole){
        sysRoleService.editSysRole(sysRole);
        return ReturnModel.successInstance();
    }

    /**
     * 查询角色信息
     * @param query
     * @return
     */
    @PostMapping("/selectList")
    public ReturnModel<List<SysRoleVo>> selectList(@RequestBody SysRoleQuery query){
        List<SysRoleVo> list = sysRoleService.selectList(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 分页查询角色信息
     * @param query
     * @return
     */
    @PostMapping("/selectPage")
    public ReturnModel<IPage<SysRoleVo>> selectPage(@RequestBody SysRoleQuery query){
        IPage<SysRoleVo> list = sysRoleService.selectPage(query);
        return ReturnModel.successInstance(list);
    }

    /**
     * 查询详细
     * @param id
     * @return
     */
    @GetMapping ("/selectById")
    public ReturnModel<SysRoleVo> selectById(@RequestParam() Long id){
        SysRoleVo roleVo = sysRoleService.selectById(id);
        return ReturnModel.successInstance(roleVo);
    }

    /**
     * 判断角色编码是否已存在
     * @param roleCode
     * @return
     */
    @GetMapping ("/checkRoleCode")
    public ReturnModel<Object> checkRoleName(@RequestParam() String roleCode){
        boolean b = sysRoleService.checkRoleCode(roleCode);
        return ReturnModel.successInstance(b);
    }

    /**
     * 查询用户角色信息和角色列表
     * @param userId
     * @return
     */
    @GetMapping("/getUserRoleList")
    public ReturnModel<AllotBo> getUserRoleList(@RequestParam() Long userId){
        AllotBo bo = sysRoleService.getUserRoleList(userId);
        return ReturnModel.successInstance(bo);
    }


    /**
     * 分配权限
     * @param form
     * @return
     */
    @PostMapping("/allotPermission")
    public ReturnModel<Object> allotPermission(@RequestBody AllotForm form){
        sysRoleService.allotPermission(form);
        return ReturnModel.successInstance();
    }

}

